package org.coder.consumer.anno;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.coder.api.EchoService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @description:
 * @author: coder97
 * @since: 2021/03/15
 */
public class AnnotationConsumer {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext applicationContext = new
                AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        applicationContext.start();

        for (int i = 0; i < 200; i++) {
            EchoService echoService = applicationContext.getBean(EchoService.class);
            final String echoResult = echoService.echo("你好,dubbo！这是第"+(i+1)+"次调用");
            System.out.println("---------");
            System.out.println(echoResult);
            System.out.println("---------");
        }
        System.out.println("-----end------");

    }

    @Configuration
    @EnableDubbo(scanBasePackages = {"org.coder.consumer.anno"})
    @ComponentScan(value = {"org.coder.consumer.anno"})
    static class ConsumerConfiguration {

        @Bean
        public ApplicationConfig applicationConfig() {

            final ApplicationConfig applicationConfig = new ApplicationConfig();
            applicationConfig.setName("echo-anno-consumer");
            return applicationConfig;

        }

        @Bean
        public RegistryConfig registryConfig() {
            final RegistryConfig registryConfig = new RegistryConfig();
            registryConfig.setAddress("localhost");
            registryConfig.setPort(2181);
            registryConfig.setProtocol("zookeeper");
            return registryConfig;
        }

        @Bean
        public EchoConsumer echoConsumer() {
            return new EchoConsumer();
        }
    }


}